package com.java.admin.orders.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.admin.makeanappointment.service.MakeanAppointmentService;
import com.java.database.entity.Employeeinfo;
import com.java.database.entity.Makeanappointmentinfo;
import com.java.database.entity.Orders;
import com.java.database.mapper.EmployeeinfoMapper;
import com.java.database.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private EmployeeinfoMapper employeeinfoMapper;
    @Autowired
    private MakeanAppointmentService makeanAppointmentService;
    //查询所有
    public PageInfo queryAll(String pageNum){
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        PageHelper.startPage(Integer.parseInt(pageNum), 5);
        List<Orders> orders = ordersMapper.selectAll();
        PageInfo page = new PageInfo(orders);
        return page;
    }
    //删除
    public void deOrders(String ordersid){
        if(ordersid==null||"".equals(ordersid)){
            throw new RuntimeException("参数为空");
        }
        ordersMapper.removeByPrimaryKey(Integer.parseInt(ordersid));
    }
    //添加
    public void addOrders(Makeanappointmentinfo makeanappointment,String employeeid){
        //创建orders对象
        Orders orders = new Orders();
        //设置orders对象的Makeanappointmentinfo属性
        orders.setMakeanappointmentinfo(makeanappointment);
        Employeeinfo employeeinfo = employeeinfoMapper.selectByPrimaryKey(Integer.parseInt(employeeid));
        //设置orders对象的Employeeinfo属性
        orders.setEmployeeinfo(employeeinfo);
        //设置orders对象的Accepttime属性
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        orders.setAccepttime(time);
        //调用添加方法
        int insert = ordersMapper.insert(orders);
        if(insert>0){
            makeanAppointmentService.updateMakeAnAppointMentAccept(makeanappointment.getMakeanappointmentid());
        }
    }
}
